﻿Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Entity
Imports System.Linq
Imports System.Net
Imports System.Web
Imports System.Web.Mvc
Imports PagedList

Namespace QuanLyKhachSan
    Public Class ProductController
        Inherits System.Web.Mvc.Controller

        Private db As New NorthwindSQLEntities

        ' GET: /Product/
        Public Function Index(ByVal sortOrder As String, _
                        ByVal currentFilter As String, _
                        ByVal searchString As String, _
                        ByVal page As Integer?) As ActionResult

            Try

                ViewBag.NameSortParm = If([String].IsNullOrEmpty(sortOrder), "productname_desc", "")
                ViewBag.CurrentSort = sortOrder

                If searchString IsNot Nothing Then
                    page = 1
                Else
                    searchString = currentFilter
                End If
                ViewBag.CurrentFilter = searchString

                Dim products = db.Products.Include(Function(p) p.Category).Include(Function(p) p.Supplier)
                Select Case sortOrder
                    Case "productname_desc"
                        products = products.OrderByDescending(Function(s) s.ProductName)
                        Exit Select
                    Case "categoryname_desc"
                        products = products.OrderByDescending(Function(s) s.Category.CategoryName)
                        Exit Select
                    Case "companyname_desc"
                        products = products.OrderByDescending(Function(s) s.Supplier.CompanyName)
                        Exit Select
                    Case Else
                        products = products.OrderByDescending(Function(s) s.ProductName)
                        Exit Select
                End Select

                'Return View(products.ToList())

                Dim pageSize As Integer = 6
                Dim pageNumber As Integer = (If(page, 1))
                Return View(products.ToPagedList(pageNumber, pageSize))

            Catch ex As Exception
                MsgBox(ex.ToString)
                Return Nothing
            End Try

        End Function

        ' GET: /Product/Details/5
        Public Function Details(ByVal id As Integer?) As ActionResult

            Try
                If IsNothing(id) Then
                    Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
                End If
                Dim product As Product = db.Products.Find(id)
                If IsNothing(product) Then
                    Return HttpNotFound()
                End If
                Return View(product)
            Catch ex As Exception
                MsgBox(ex.ToString)
                Return Nothing
            End Try

        End Function

        ' GET: /Product/Create
        Public Function Create() As ActionResult
            ViewData("ActionType") = "Create"
            ViewBag.CategoryID = New SelectList(db.Categories, "CategoryID", "CategoryName")
            ViewBag.SupplierID = New SelectList(db.Suppliers, "SupplierID", "CompanyName")
            'Return View()
            Return PartialView("_Update")
        End Function

        ' POST: /Product/Create
        'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        <HttpPost()>
        <ValidateAntiForgeryToken()>
        Public Function Create(<Bind(Include:="ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")> ByVal product As Product) As ActionResult

            Try
                If ModelState.IsValid Then
                    db.Products.Add(product)
                    db.SaveChanges()
                    Return RedirectToAction("Index")
                End If
                ViewBag.CategoryID = New SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID)
                ViewBag.SupplierID = New SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID)
                Return View(product)
            Catch ex As Exception
                MsgBox(ex.ToString)
                Return Nothing
            End Try

        End Function

        ' GET: /Product/Edit/5
        Public Function Edit(ByVal id As Integer?) As ActionResult
            Try
                ViewData("ActionType") = "Edit"
                If IsNothing(id) Then
                    Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
                End If
                Dim product As Product = db.Products.Find(id)
                If IsNothing(product) Then
                    Return HttpNotFound()
                End If
                ViewBag.CategoryID = New SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID)
                ViewBag.SupplierID = New SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID)
                'Return View(product)
                Return PartialView("_Update", product)
            Catch ex As Exception
                MsgBox(ex.ToString)
                Return Nothing
            End Try

        End Function

        ' POST: /Product/Edit/5
        'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        <HttpPost()>
        <ValidateAntiForgeryToken()>
        Function Edit(<Bind(Include:="ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")> ByVal product As Product) As ActionResult
            Try
                If ModelState.IsValid Then
                    db.Entry(product).State = EntityState.Modified
                    db.SaveChanges()
                    Return RedirectToAction("Index")
                End If
                ViewBag.CategoryID = New SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID)
                ViewBag.SupplierID = New SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID)
                Return View(product)

            Catch ex As Exception
                MsgBox(ex.ToString)
                Return Nothing
            End Try

        End Function

        ' GET: /Product/Delete/5
        Function Delete(ByVal id As Integer?) As ActionResult
            If IsNothing(id) Then
                Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
            End If
            Dim product As Product = db.Products.Find(id)
            If IsNothing(product) Then
                Return HttpNotFound()
            End If
            Return View(product)
        End Function

        ' POST: /Product/Delete/5
        <HttpPost()>
        <ActionName("Delete")>
        <ValidateAntiForgeryToken()>
        Function DeleteConfirmed(ByVal id As Integer) As ActionResult
            Dim product As Product = db.Products.Find(id)
            db.Products.Remove(product)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End Function

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing) Then
                db.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub
    End Class
End Namespace
